package cz.ctu.android.DriveCommunicator.activities;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


import android.app.Activity;
import android.content.Intent;
import android.graphics.YuvImage;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import cz.ctu.android.DriveCommunicator.R;
import cz.ctu.android.DriveCommunicator.utils.DriverConstants;
import cz.ctu.android.DriveCommunicator.utils.HandlerMessage;
import cz.ctu.android.DriveCommunicator.utils.ImageMapper;
import cz.ctu.android.DriveCommunicator.utils.Zprava;
import cz.ctu.android.DriveCommunicator.utils.DriverConstants.Users;
import cz.ctu.android.adapters.CustomListViewAdapter;
import cz.ctu.android.entity.RowItem;

/**
 * 
 * @author karlos
 */
public class ConversationActivity extends Activity implements
		OnItemClickListener {
	
	RadioButton conversationRadio;
	RadioButton unreadRadio;
	ListView listView;
	List<RowItem> rowItems;
	TextView text;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.conversations);

		loadScreen();


	}
	@Override
	public void onResume(){
		super.onResume();
		loadScreen();
	}
	
	public void loadScreen(){
		text = (TextView)findViewById(R.id.noConversationFoundText);

		listView = (ListView) findViewById(R.id.tomList);
		conversationRadio = (RadioButton)findViewById(R.id.conversationRadio);
		unreadRadio = (RadioButton)findViewById(R.id.unreadMessage);
		unreadRadio.setOnClickListener(unreadMessageClick);
			conversationRadio.setChecked(true);
			unreadRadio.setChecked(false);

		HandlerMessage hl = new HandlerMessage();
		rowItems = new ArrayList<RowItem>();

		for (int i = 1; i < DriverConstants.numberOfUsers+1; i++) {
			ArrayList<Zprava> conversation =  hl.getConversetion(i);
			if(conversation != null && !conversation.isEmpty()){
				Zprava zprava = conversation.get(0);
				RowItem item;
				String sender = null;
				if(zprava.getSenderID() == DriverConstants.myId){
					sender = Users.getUserByID(zprava.getId()).getJmeno()+" " + Users.getUserByID(zprava.getId()).getPrijmeni() +" -sended";
				}
				else if(!zprava.isPrectena()){
						sender = zprava.getJmeno() +" " +zprava.getPrijmeni() +" *";
					}
				else{
					sender = zprava.getJmeno() + " "+ zprava.getPrijmeni();
				}
				item = new RowItem(ImageMapper.getImage(i), i,sender , zprava.getZprava());	
				rowItems.add(item);
			}
		}
		if(rowItems != null && !rowItems.isEmpty()){
			text.setVisibility(3);
			
			CustomListViewAdapter adapter = new CustomListViewAdapter(this,
					R.layout.list_item, rowItems);
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(this);
		}
		else{
			text.setText("No conversation found");
			text.setVisibility(1);
			listView.setVisibility(3);
		}
	}

	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Toast toast = Toast.makeText(getApplicationContext(), "Item "
				+ (position + 1) + ": " + rowItems.get(position),
				Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
		toast.show();
		CustomListViewAdapter cw = (CustomListViewAdapter)parent.getAdapter();
		RowItem row = cw.getItem(position);
		HandlerMessage hm = new HandlerMessage();
		hm.markAsRead(row.getId());
		Bundle bundle = new Bundle();
		bundle.putInt("idToLoad", row.getId());
		Intent newIntent = new Intent(this.getApplicationContext(), MessageActivity.class);
		newIntent.putExtras(bundle);
        startActivity(newIntent);
	}
	
	private OnClickListener unreadMessageClick = new OnClickListener() {
		
		public void onClick(View v) {
		Intent intent = new Intent(ConversationActivity.this, UnreadMessage.class);
			startActivity(intent);
		}
	};
	
}
